package cn.wolfcode;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JWTUtils {
    @Value("${jwt.sign}")
    private String sign;
    @Value("${jwt.outTime}")
    private Integer outTime;

    //生成 JWT
    public String generateJWT(Map<String, Object> claims) {
        return Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, sign)
                .setExpiration(new Date(System.currentTimeMillis() + outTime))
                .compact();
    }

    //解析 JWT
    public Claims parserJWT(String jwt) {
        return Jwts.parser()
                .setSigningKey(sign)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
